//
//  myView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/15.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "myView.h"

@interface myView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;

@end

@implementation myView

- (UITableView *)myTableView
{
    if(!_myTableView){
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
                                                                     0,
                                                                     self.bounds.size.width,
                                                                     self.bounds.size.height)
                                                    style:UITableViewStyleGrouped];
        [_myTableView setBackgroundColor:BACKGROUND_COLOR];
        _myTableView.tableFooterView = [[UIView alloc] init];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}

#pragma mark - UITableViewDataSource 协议
//对应第section组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0){
        return 3;
    }else{
        return 2;
    }
}

//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *customView;
    
    if(section == 0){
        customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, self.bounds.size.width, 20.0)];
    }else{
        customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, self.bounds.size.width, 44.0)];
    }
    
    UILabel * headerLabel = [[UILabel alloc] init];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor lightGrayColor];
    headerLabel.highlightedTextColor = [UIColor whiteColor];
    headerLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:24];
    if(section == 0){
        headerLabel.text = @"防御设施";
        headerLabel.frame = CGRectMake(10.0, 0.0, self.bounds.size.width, 10.0);
    }else{
        headerLabel.text = @"语言与网络";
        headerLabel.frame = CGRectMake(10.0, 0.0, self.bounds.size.width, 44.0);
    }
    
    [customView addSubview:headerLabel];
    
    return customView;
}

//每一行的设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIndentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIndentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [cell.textLabel setFont:[UIFont systemFontOfSize:20]];
    [cell.textLabel setTextColor:[UIColor blackColor]];
    UIImage *image;
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            [cell.textLabel setText:@"安防查看"];
            [cell.detailTextLabel setText:@"设置每个房间的安全防御"];
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_safe");
        }else if(indexPath.row == 1){
            [cell.textLabel setText:@"实时监控"];
            [cell.detailTextLabel setText:@"开启和关闭实时监控"];
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_monitor");
        }else if(indexPath.row == 2){
            [cell.textLabel setText:@"触摸声音"];
            [cell.detailTextLabel setText:@"开启和关闭触摸声音"];
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_monitor");
        }
    }else if(indexPath.section == 1){
        if(indexPath.row == 0){
            [cell.textLabel setText:@"语言"];
            [cell.detailTextLabel setText:@"设置用户操作语言"];
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_lang");
        }else if(indexPath.row == 1){
            [cell.textLabel setText:@"通信"];
            [cell.detailTextLabel setText:@"设置用户的通信方式"];
            image = GET_IAMGE_WITH_NAME(@"Image/tableView/list_txway");
        }
    }
    
    if((indexPath.section == 0 && indexPath.row == 0) || indexPath.section == 1){
        UIImage *rightImage = GET_IAMGE_WITH_NAME(@"Image/tableView/monitor_go");
        UIImageView *rightView = [[UIImageView alloc] initWithImage:rightImage];
        [rightView setFrame:CGRectMake(self.bounds.size.width - 35 - 20, 20, 35, 35)];
        [cell addSubview:rightView];
    }else{
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISwitch *mySwith = [[UISwitch alloc] initWithFrame:CGRectMake(self.bounds.size.width - 51 - 20, 22, 51, 31)];
        mySwith.on = NO;
        if(indexPath.row == 1){
            
        }else if (indexPath.row == 2){
            
        }
        //NSLog(@"%f,%f",mySwith.bounds.size.width,mySwith.bounds.size.height);
        [cell addSubview:mySwith];
    }
    
    cell.textLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:22];
    cell.detailTextLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0];
    cell.imageView.image = image;
    
    //改变图像大小
    CGSize itemSize = CGSizeMake(30, 30);
    UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

//选择了相应行后的操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if((indexPath.section == 0 && indexPath.row == 0) || indexPath.section == 1){
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

#pragma mark - UITableViewDelegate 协议

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}
//每组组名的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0){
        return 24;
    }
    return 44;
}

- (id) initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.myTableView];
    }
    return self;
}

@end
